package com.songcha.module_mine.ui.activity.bind_phone

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_base.mvvm.base.beanFlatMap
import com.songcha.library_business.api.ApiManager
import com.songcha.library_business.bean.user.UserInfoBean
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_network.bean.BaseBean

class BindPhoneViewModel(app: Application):BaseViewModel<BindPhoneRepository>(app) {

    //59-0
    val verifyCountDown=MutableLiveData(0)

    val sendVerifySuccess=MutableLiveData<Boolean>()

    val bindPhoneState=MutableLiveData<Boolean>()

    var isChangeBindPhone=false

    fun changeBindPhone(phone:String,code:String){
        val obs=repository!!.changeBindPhone(phone,code)
            .beanFlatMap {
                return@beanFlatMap ApiManager.api.getUserInfo()
            }

        handleApiDataObserver(obs,object : IApiResult<UserInfoBean>{
            override fun onSuccess(bean: UserInfoBean) {
                UserInfoManager.saveUserInfo(bean.data)
                bindPhoneState.value=true
            }

            override fun onError(e: Throwable) {
                bindPhoneState.value=false
            }

        })
    }


    fun sendVerify(phone:String){
        handleApiDataObserver(repository!!.sendVerifyCode(phone),object : IApiResult<BaseBean>{
            override fun onSuccess(bean: BaseBean) {
                //verifyCountDown.value=0
                sendVerifySuccess.value=true
            }

            override fun onError(e: Throwable) {
                verifyCountDown.value=0
                sendVerifySuccess.value=false
            }

        })
    }

    fun setCountDown(countDown:Int){
        verifyCountDown.value=countDown
    }

}